/*
 * SliderDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.Label;
import com.cute4j.swing.ScrollPane;
import com.cute4j.swing.Separator;
import com.cute4j.swing.Slider;
import com.cute4j.swing.events.ChangeEvent;
import com.cute4j.swing.events.ChangeListener;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the Slider class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class SliderDemo extends DemoModule {

// Private Fields. -------------------------------------------------------------

  private Label sliderValue;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new SliderDemo object.
   */
  public SliderDemo() {
    super("Slider Demo", "Demonstration of the Slider class.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    FormLayout layout = new FormLayout(
                  "200dlu, 20dlu, 4dlu, p, 8dlu, p, 8dlu, p",
                  "p, 6dlu, p, 12dlu, p, 6dlu, p, 12dlu, p, 6dlu, p, 24dlu, p");
    Container panel = new Container(layout);
    panel.setInsets(16, 16, 16, 16);
    SliderListener sliderListener = new SliderListener();
    CellConstraints cc = new CellConstraints();

    panel.add(new Separator("Horizontal"), cc.xy(1, 1));
    Slider slider = new Slider();
    slider.addChangeListener(sliderListener);
    panel.add(slider, cc.xy(1, 3));

    panel.add(new Separator("Disabled"), cc.xy(1, 5));
    slider = new Slider();
    slider.setEnabled(false);
    panel.add(slider, cc.xy(1, 7));

    panel.add(new Separator("Ticks"), cc.xy(1, 9));
    slider = new Slider();
    slider.addChangeListener(sliderListener);
    slider.setPaintTicks(true);
    panel.add(slider, cc.xy(1, 11));

    panel.add(new Separator("Vertical"), cc.xy(4, 1));
    slider = new Slider(Slider.VERTICAL);
    slider.addChangeListener(sliderListener);
    panel.add(slider, cc.xyh(4, 3, 9, "c, f"));

    panel.add(new Separator("Disabled"), cc.xy(6, 1));
    slider = new Slider(Slider.VERTICAL);
    slider.setEnabled(false);
    panel.add(slider, cc.xyh(6, 3, 9, "c, f"));

    panel.add(new Separator("Ticks"), cc.xy(8, 1));
    slider = new Slider(Slider.VERTICAL);
    slider.addChangeListener(sliderListener);
    slider.setPaintTicks(true);
    panel.add(slider, cc.xyh(8, 3, 9, "c, f"));

    panel.add(new Label("Slider Value:"), cc.xyw(1, 13, 2, "r, b"));
    sliderValue = new Label("50");
    panel.add(sliderValue, cc.xy(4, 13, "l, b"));

    return new ScrollPane(panel);
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * Slider Listener.
   */
  class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
      Slider slider = (Slider)e.getSource();
      sliderValue.setText(String.valueOf(slider.getValue()));
    }
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new SliderDemo().show();
  }

}
